/* Copyright Andy Mitchell 2007. No warranties */

function ConversationUI(gtdinboxPage){
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	this._labels = gtdinboxPage._labels;
	this._multiToOne = new MultiToOne(gtdinboxPage, 200);
	
	this._eventObservers = new EventObservers(this);
	
	this._labels.addEventHandler( this, "actionlist-manual-click", this.manualThreadLabelChange );
	//this._labels.addEventHandler( this, "refresh-labels", this.refreshLabels );
	
}

ConversationUI.prototype.viewChange = function(gmail, callback){
try{
	// Make sure we're still embedded
	
	var doc = this._gtdinboxPage.gmailDOM.element("document", gmail);
	

		
	var match = this._doc.location.href.match(/\/#inbox\/(.+)$/);
	if( !match ) match = this._doc.location.href.match(/\/#search\/.+\/(.+)$/);
	var id = match? match[1] : "";
	var thread = id? this._gtdinboxPage._threadList.getThreadById(id) : null;
	if( thread ) this.thread = thread;
	if( !this.thread && !callback ){	// Just fake it
		this.thread = new Thread(this._gtdinboxPage._threadList, id);
		var elSubject = this._gtdinboxPage.gmailDOM.element("conversation-subject");
		this.thread.subject = elSubject.innerHTML;
		this._gtdinboxPage._threadList.addThread(this.thread, true);
		this._labels.getActiveLabels(this, function(activeLabels){
				this.thread.verifyLabels(activeLabels);
				this.viewChange(gmail, true);
			});
		return;
	}
	
	
	this.thread.initEventHandler();
	this.thread.eventHandlers.reset();	// get it exclusively for us
		
	/*
	this.thread.addEventHandler(this, "addLabel", this.threadUpdate);
	this.thread.addEventHandler(this, "removeLabel", this.threadUpdate);
	this.thread.addEventHandler(this, "update-body", this.threadUpdate);
	*/
	
	// Label Selection Header
	if( this.elH1 ){
		this._eventObservers.stopObserving( this.elH1, "click", this._clickHeader, false );
		this._eventObservers.stopObserving( this.elH1, "DOMNodeInserted", this._modifyHeader, false );
		this._eventObservers.stopObserving( this.elH1, "DOMNodeRemoved", this._modifyHeader, false );
	}
	this.elH1 = this._gtdinboxPage.gmailDOM.element("conversation-header");
	if( !this.elH1 ) return;
	this._eventObservers.observe( this.elH1, "click", this._clickHeader, false );
	this._eventObservers.observe( this.elH1, "DOMNodeInserted", this._modifyHeader, false );
	this._eventObservers.observe( this.elH1, "DOMNodeRemoved", this._modifyHeader, false );
	
	var elHeader = this.elH1.parentNode;
	if( elHeader.getElementsByTagName("DIV")[0] && elHeader.getElementsByTagName("DIV")[0].className.indexOf("conversation-header")>-1 ){	// already modified
		return;
	}
	
	var elConversationHeader = this._doc.createElement("DIV");
		elConversationHeader.className = "conversation-header";
	elHeader.appendChild( elConversationHeader );
	
	if( !this._labelSelect ){
		this._threadActions = new ThreadActionsUI(this._gtdinboxPage);
		this._threadActions.refresh(this.thread);
		this._labelSelect = new LabelSelectUI( this._gtdinboxPage, null, null, this._threadActions );
		this._labelSelect.addEventHandler(this, "click-label", this.toggleLabel);
		
		this._showHiderLabels = new Showhider(this._gtdinboxPage, "conversation-labels", {doc: this._doc, title:this._gtdinboxPage.stringBundle.getString("Conversation.Labels"), elContent: this._labelSelect.el, overrideShow:false });
		this._showHiderLabels.addEventHandler(this, "show", this.refreshLabels);
	}
	elConversationHeader.appendChild( this._showHiderLabels.el );
	if( this._showHiderLabels.show() ) this.refreshLabels();
	
	// Detect clicks to conversation
	if( this._elConversation ){
		this._eventObservers.stopObserving( this._elConversation, "click", this._clickConversation, false );
	}
	this._elConversation = this._gtdinboxPage.gmailDOM.element("ActiveViewElement");
	this._eventObservers.observe( this._elConversation, "click", this._clickConversation, false );
	
	// Add in the Finish button
	var elArchive = this._gtdinboxPage.gmailDOM.element("archive-button");
	if( elArchive ){	
		var elFinish = this._threadActions.getFinishedButton();
		elFinish.className = elArchive.className;
		elArchive.parentNode.insertBefore( elFinish, elArchive.nextSibling );
	}
	
	if( thread && thread.checkAutoLabel ) thread.checkAutoLabel();
	
}catch(e){
	if( GTDDEBUGMODE ) alert("conversationui viewChange\n" + e.toString());
}
};

ConversationUI.prototype._modifyHeader = function(event){
	this._multiToOne.process("modifyHeader", GIPROTOTYPE.bind(this, this.refreshLabels));
};
ConversationUI.prototype._clickHeader = function(event){
	var el = GIPROTOTYPE.element(event);
	
	if( this._gtdinboxPage.gmailDOM.test("conversation-x",el) ){	// It's a remove
		this.refreshLabels();
	}	
};
ConversationUI.prototype._clickConversation = function(event){
	var el = GIPROTOTYPE.element(event);
	
	// See if star was clicked & respond
	if( this._gtdinboxPage.settings.labels.starForNextAction && this._gtdinboxPage.gmailDOM.test("star", el) ){	// It's a star
		// Test to see if star is on/off and respond
		var settings = this._gtdinboxPage.settings;
		var labelNextAction = settings.labels.types.Statuses.prefix + settings.labels.types.Statuses.defaults.NextAction;
		if( this._gtdinboxPage.gmailDOM.test("star-on", el) ){	// its on, add label
			this._gtdinboxPage._labels.labelThread({labelname:labelNextAction, 'on':true, 'threads':this.thread});
		}else{	// Off, make sure it's off
			this._gtdinboxPage._labels.labelThread({labelname:labelNextAction, 'on':false, 'threads':this.thread});
		}
	}

};

ConversationUI.prototype.refreshLabels = function(){
try{
	this._gtdinboxPage._threadListUI.uncheckOutOfView();
	this._labels.getActiveLabels( this, this._refreshLabels );
}catch(e){
	if( GTDDEBUGMODE ) alert("conversation ui refreshLabels\n" + e.toString());
}
};
ConversationUI.prototype._refreshLabels = function(activeLabels){
	if( this.thread ) this.thread.verifyLabels( activeLabels );
	this._labelSelect.refresh(this.thread);
};

/*
ConversationUI.prototype.threadUpdate = function(){	
try{
	this.thread.verifyLabels( this._labels.getActiveLabels() );
	if( this._gtdinboxPage.view("view")!="cv" ) return;
	this._labelSelect.refresh(this.thread);
	this._labelSelect.newLabels( this.thread.getUnknownBodyLabels() );
}catch(e){
	if( GTDDEBUGMODE ) alert("conversation ui threadUpdate\n" + e.toString());
}
};
*/
ConversationUI.prototype.manualThreadLabelChange = function(event, label){
	// Called when user manually selects label on drop down
	// Kept seperate from Labels because ThreadList/Conversation would handle it differently

try{
	if( this._gtdinboxPage.view("view")!="cv" ) return;	// i should be active view

	if( this.thread ){
		if( label.act=="12" ){
			var callback = null;
			if( label.label==(this._gtdinboxPage.settings.labels.types.Statuses.prefix + this._gtdinboxPage.settings.labels.types.Statuses.defaults.Finished) ){	// clicked finish, archive it
				callback = GIPROTOTYPE.bind(this, function(){
						var threadActionsUI = new ThreadActionsUI(this._gtdinboxPage, null, true);
						threadActionsUI._clickArchive();
					});
			}
			this.thread.addLabel(label.label, null, callback);
		}else if(label.act=="13"){
			this.thread.removeLabel(label.label);
		}
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("ConversationUI manualThreadLabelChange\n" + e.toString());
}
};

ConversationUI.prototype.toggleLabel = function(e, label){
try{
	var labelname = label.labelname;
	var fCallback = GIPROTOTYPE.bind(this, function(act){
			if( act > 0 ){
				this._labelSelect.selected( this.thread.labels );
			}
		});
	this._gtdinboxPage._labels.labelThread({'labelname':labelname, 'on':undefined, 'threads':this.thread, 'fCallback':fCallback});
}catch(e){
	if( GTDDEBUGMODE ) alert("conversationui toggleLabel\n" + e.toString());
}
};

ConversationUI.prototype.destroy = function(){
	if( this._labelSelect ) this._labelSelect.destroy();
	if( this._showHiderLabels) this._showHiderLabels.destroy();
	this._multiToOne.destroy();

	this._eventObservers.destroy();
	
	this.elH1 = null;
	this.thread = null;
	this._labels = null;
	this._doc = null;
	this._gtdinboxPage = null;
}